#include "String.h"

namespace SUNSHINE
{
	string String::Concatenate(const vector<string> & v,
				const string & delimiter)
	{
		if (v.empty())
		{
			return "";
		}

		string result(v[0]);
		vector<string>::const_iterator iter;

		for (iter = v.begin() + 1; iter != v.end(); ++iter)
		{
			result.append(delimiter).append(*iter);
		}

		return result;
	}

	vector<string> String::Split(const string & s, const string & delimiter)
	{
		vector<string> result;

		if (s.empty())
		{
			return result;
		}

		if (delimiter.empty())
		{
			result.push_back(s);
			return result;
		}

		string::size_type delimiterLength = delimiter.size();
		string::size_type current = 0;
		string::size_type next = 0;

		while ((next = s.find(delimiter, current)) != string::npos)
		{	
			result.push_back(s.substr(current, next - current));
			current = next + delimiterLength;
		}
		result.push_back(s.substr(current, s.size() - current));

		return result;

	}
}

